package com.design.blog.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * @author hxg
 * @version 1.0
 * @date 2022/3/31 21:40
 */
public class ImgUtils {
    public static Object upload(MultipartFile file, HttpSession session) throws IOException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", "b4df0e9b229458bf8a41efb55048acdf");
        paramMap.put("owner", "hniko");
        paramMap.put("repo", "config-center");
        String uuid = IdUtil.randomUUID();
        paramMap.put("path", uuid+".jpg");

        String encode = Base64.encode(new File("C:\\Users\\24373\\Desktop\\"+file.getOriginalFilename()));

        paramMap.put("content", encode);
        paramMap.put("message", "文章上传"+ LocalDateTime.now());
        String result= HttpUtil.post("https://gitee.com/api/v5/repos/hniko/config-center/contents/"+uuid+"_"+file.getOriginalFilename(), paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        Object content = jsonObject.get("content");
        JSONObject contentObj = JSONUtil.parseObj(content);
        return contentObj.get("download_url");
    }
}
